package com.have.blog.xo.uitls;


import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.enums.FilePriority;
import com.have.blog.base.exception.exceptionType.QueryException;
import com.have.blog.model.mapper.TSystemConfigMapper;
import com.have.blog.model.models.TSystemConfig;
import com.have.blog.model.models.TSystemConfigExample;
import com.have.blog.utils.JsonUtils;
import com.have.blog.xo.constant.MessageConstant;
import com.have.blog.xo.constant.SysConstant;
import com.have.blog.xo.service.cache.SystemConfigCacheService;
import com.have.blog.xo.service.admin.SystemConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
@SuppressWarnings("all")
public class WebUtil {
    @Autowired
    private SystemConfigService systemConfigService;
    @Autowired
    private SystemConfigCacheService systemConfigCacheService;
    @Autowired
    private TSystemConfigMapper systemConfigMapper;

    /**
     * 格式化数据获取图片列表o
     * @param result
     * @return
     */
    public List<String> getPicture(String result) {
        String picturePriority = "";
        String localPictureBaseUrl = "";
        String qiNiuPictureBaseUrl = "";
        String minioPictureBaseUrl = "";

        // 获取系统配置
        TSystemConfig systemConfig = getSystemConfig();
        picturePriority = systemConfig.getPicturePriority();
        localPictureBaseUrl = systemConfig.getLocalPictureBaseUrl();
        qiNiuPictureBaseUrl = systemConfig.getQiNiuPictureBaseUrl();
        minioPictureBaseUrl = systemConfig.getMinioPictureBaseUrl();

        List<String> picUrlList = new ArrayList<>();
        try {
            Map<String, Object> picMap = (Map<String, Object>) JsonUtils.jsonToObject(result, Map.class);
            if (SysConstant.SUCCESS.equals(picMap.get(SysConstant.CODE))) {
                List<Map<String, Object>> picDataList = (List<Map<String, Object>>) picMap.get(SysConstant.DATA);
                if (!CollectionUtils.isEmpty(picDataList)) {
                    for (Map<String, Object> item : picDataList) {
                        if (FilePriority.QI_NIU.equals(picturePriority)) {
                            picUrlList.add(qiNiuPictureBaseUrl + item.get(SysConstant.QI_NIU_URL));
                        } else if (FilePriority.MINIO.equals(picturePriority)) {
                            picUrlList.add(minioPictureBaseUrl + item.get(SysConstant.MINIO_URL));
                        } else {
                            picUrlList.add(localPictureBaseUrl + item.get(SysConstant.URL));
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("从Json中获取图片列表失败");
            log.error(e.getMessage());
        } finally {
            return picUrlList;
        }
    }

    /**
     * 获取图片
     * @param result
     * @return
     */
    public List<Map<String, Object>> getPictureMap(String result) {
        String picturePriority = "";
        String localPictureBaseUrl = "";
        String qiNiuPictureBaseUrl = "";
        String minioPictureBaseUrl = "";
        // 1. 获取系统配置
        TSystemConfig systemConfig = getSystemConfig();
        picturePriority = systemConfig.getPicturePriority();
        localPictureBaseUrl = systemConfig.getLocalPictureBaseUrl();
        qiNiuPictureBaseUrl = systemConfig.getQiNiuPictureBaseUrl();
        minioPictureBaseUrl = systemConfig.getMinioPictureBaseUrl();

        List<Map<String, Object>> resultList= new ArrayList<>();
        Map<String, Object> picMap = (Map<String, Object>) JsonUtils.jsonToObject(result, Map.class);
        if (SysConstant.SUCCESS.equals(picMap.get(SysConstant.CODE))) {
            List<Map<String, Object>> picDataList = (List<Map<String, Object>>) picMap.get(SysConstant.DATA);
            if (!CollectionUtils.isEmpty(picDataList)) {
                for (Map<String, Object> item : picDataList) {
                    Map<String, Object> map = new HashMap<>();
                    if (StringUtils.isEmpty(item.get(SysConstant.UID))) {
                        continue;
                    }
                    // 设置文件
                    if (FilePriority.QI_NIU.equals(picturePriority)) {
                        map.put(SysConstant.URL, qiNiuPictureBaseUrl + item.get(SysConstant.QI_NIU_URL));
                    } else if (FilePriority.MINIO.equals(picturePriority)) {
                        map.put(SysConstant.URL, minioPictureBaseUrl + item.get(SysConstant.MINIO_URL));
                    } else {
                        map.put(SysConstant.URL, localPictureBaseUrl + item.get(SysConstant.URL));
                    }
                    map.put(SysConstant.UID, item.get(SysConstant.UID));
                    resultList.add(map);
                }
            }
        } else if (SysConstant.ERROR.equals(picMap.get(SysConstant.CODE))) {
            log.error("获取图片失败，图片服务出现异常：{}", picMap.get(SysConstant.MESSAGE));
        } else {
            log.error("获取图片失败");
        }
        return resultList;
    }


    public TSystemConfig getSystemConfig() {
        TSystemConfig systemConfig = systemConfigCacheService.getSystemConfig();
        if (systemConfig == null) {
            TSystemConfigExample example = new TSystemConfigExample();
            example.createCriteria()
                    .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
            List<TSystemConfig> configs = systemConfigMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(configs)) {
                throw new QueryException(MessageConstant.SYSTEM_CONFIG_IS_NOT_EXIST);
            } else {
                systemConfig = configs.get(0);
                systemConfigCacheService.setSystemConfig(systemConfig);
            }
        }
        return systemConfig;
    }
}
